package com.jwsoft.manager.core.integration.addressParse;

import cn.hutool.core.collection.CollUtil;
import com.jwsoft.manager.common.enums.AreaEnum;
import com.jwsoft.manager.common.vo.addressParse.Address;
import com.jwsoft.manager.common.vo.addressParse.AddressInfo;

import java.util.ArrayList;
import java.util.List;

public class AddressDataUtil {


    /**
     * 获取制定级别的数据
     *
     * @param data  数据源
     * @param level 制定级别，从0开始
     * @return
     */
    public static List<Address> getAssignLevel(List<Address> data, int level, AddressInfo addressInfo) {
        return getAssignLevel(data, level, 0,addressInfo);
    }

    public static Address getAssignLevel(List<Address> data,int currLevel, String areaCode) {
        Address address=null;
        if (currLevel==3){
            return null;
        }
        for (Address datum : data) {
            if (areaCode.equalsIgnoreCase(datum.getAreaCode())){
                address=datum;
                break;
            }
            if (currLevel<3&&CollUtil.isNotEmpty(datum.getChildren())) {
                address=getAssignLevel(datum.getChildren(),currLevel + 1,areaCode);
                if (address!=null){
                    break;
                }
            }
        }
        return address;
    }
    private static List<Address> getAssignLevel(List<Address> data, int level, int currLevel,AddressInfo addressInfo) {
        if (level == currLevel) {
            return data;
        }
        List<Address> result = new ArrayList<>();
        for (Address datum : data) {
            if (currLevel==0){
                if (addressInfo.getProvinceId()!=null){
                    if (addressInfo.getProvinceId()-datum.getAddressId()!=0){
                        continue;
                    }
                }
            }else if(currLevel==1){
                if (addressInfo.getCityId()!=null){
                    if (addressInfo.getCityId()-datum.getAddressId()!=0){
                        continue;
                    }
                }
            }else if(currLevel==2){
                if (addressInfo.getDistrictId()!=null){
                    if (addressInfo.getDistrictId()-datum.getAddressId()!=0){
                        continue;
                    }
                }
            }else if(currLevel==3){
                if (addressInfo.getTownId()!=null){
                    if (addressInfo.getTownId()-datum.getAddressId()!=0){
                        continue;
                    }
                }
            }else {
                if (datum.getAreaType().equalsIgnoreCase(AreaEnum.village.getType())&&addressInfo.getVillageId()!=null){
                    if (addressInfo.getVillageId()-datum.getAddressId()!=0){
                        continue;
                    }
                }
                if (datum.getAreaType().equalsIgnoreCase(AreaEnum.road.getType())&&addressInfo.getRoadId()!=null){
                    if (addressInfo.getRoadId()-datum.getAddressId()!=0){
                        continue;
                    }
                }
            }
            if (CollUtil.isNotEmpty(datum.getChildren())) {
                result.addAll(getAssignLevel(datum.getChildren(), level, currLevel + 1,addressInfo));
            }
        }
        return result;
    }





}
